﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ebc.patterns
{
    public class HeadTailSplitter<T>
    {
        private Predicate<T> endOfListCheck;
        private bool startOfList;


        public HeadTailSplitter(Predicate<T> endOfListCheck)
        {
            this.endOfListCheck = endOfListCheck;
            this.startOfList = true;
        }


        public void ProcessList(T element)
        {
            if (this.endOfListCheck(element))
            {
                this.OnTail(element);
                this.startOfList = true;
            }
            else
            {
                if (this.startOfList)
                    this.OnHead(element);
                else
                    this.OnTail(element);

                this.startOfList = false;
            }
        }


        public event Action<T> OnHead;
        public event Action<T> OnTail;
    }
}
